package cn.wzy.rocketmq.listener;

import cn.wzy.constants.GiftConstants;
import cn.wzy.doc.GoodsSpuOnsaleDetailDoc;
import cn.wzy.service.IGoodsSpuOnsaleDetailDocService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
@RocketMQMessageListener(topic = GiftConstants.OFFSALE_TX_PRODUCER_TOPIC,
        selectorExpression="*",consumerGroup = "offsale_cusumer_group_test")
public class GiftOffsaleDataSyncListener implements RocketMQListener<MessageExt> {

    @Autowired
    private IGoodsSpuOnsaleDetailDocService service;
    @Override
    public void onMessage(MessageExt messageExt) {

        //获取消息体
        byte[] body = messageExt.getBody();

        //获取json字符串
        try {
            String jsonStr = new String(body, "utf-8");

            //解析为对象
            List<Long> ids = JSONObject
                    .parseArray(jsonStr, Long.class);


            //批量查询
            List<GoodsSpuOnsaleDetailDoc> docs = service.getByIds(ids);

            //批量删除
            service.batchDel(docs);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}